/*
 * Copyright (c) 2016. For Intelligent Group.
 */
package com.intelligent.ispc.client.core.view.analysis.spc;

import com.intelligent.ispc.client.core.modal.spc.TestItemTableModel;
import com.intelligent.ispc.client.utils.Colur;
import com.intelligent.ispc.constant.AppConstant;


import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

/**
 * Created by Tony.yu on 2016/10/11.
 */
public class TestDataTableRender extends DefaultTableCellRenderer {

    private TestItemTableModel testItemTableModel;

    /**
     * Constructor
     *
     * @param table table
     */
    public TestDataTableRender(JTable table) {

        testItemTableModel = (TestItemTableModel) table.getModel();

    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean isSelected, boolean hasFocus, int row, int column) {
        String resultName = AppConstant.SPC_TEST_DATA_TABLE_TITLE[6];

        if (resultName.equals(table.getColumnName(column))) {

            String result = table.getValueAt(row, column).toString();
            setBackColor(result);
        } else {
            setBackground(Colur.WHITE);
            setForeground(Colur.BLACK);
        }
        setHorizontalAlignment(CENTER);

        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }

    private void setBackColor(String result) {
        if ("Pass".equals(result)) {
            setBackground(Colur.GREEN);
            setForeground(Colur.WHITE);
        } else if ("Fail".equals(result)) {
            setBackground(Colur.RED);
            setForeground(Colur.WHITE);
        } else {
            setBackground(Colur.WHITE);
        }
    }
}
